/*
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

package com.mirth.connect.connectors.jms;

import com.mirth.connect.client.ui.UIConstants;
import com.mirth.connect.client.ui.panels.connectors.ConnectorSettingsPanel;
import com.mirth.connect.donkey.model.channel.ConnectorProperties;

public class JmsSender extends ConnectorSettingsPanel {
    public JmsSender() {
        initComponents();
        jmsConnectorPanel.init(JmsConnectorPanel.TYPE_SENDER, getConnectorName());
    }

    @Override
    public String getConnectorName() {
        return new JmsDispatcherProperties().getName();
    }

    @Override
    public ConnectorProperties getProperties() {
        JmsDispatcherProperties properties = (JmsDispatcherProperties) jmsConnectorPanel.getProperties();
        properties.setTemplate(templateTextArea.getText());
        return properties;
    }

    @Override
    public void setProperties(ConnectorProperties properties) {
        jmsConnectorPanel.setProperties(properties);
        JmsDispatcherProperties jmsDispatcherProperties = (JmsDispatcherProperties) properties;
        templateTextArea.setText(jmsDispatcherProperties.getTemplate());
    }

    @Override
    public ConnectorProperties getDefaults() {
        return new JmsDispatcherProperties();
    }

    @Override
    public boolean checkProperties(ConnectorProperties properties, boolean highlight) {
        boolean valid = jmsConnectorPanel.checkProperties(properties, highlight);

        JmsDispatcherProperties jmsDispatcherProperties = (JmsDispatcherProperties) properties;

        if (jmsDispatcherProperties.getTemplate().isEmpty()) {
            valid = false;

            if (highlight) {
                templateTextArea.setBackground(UIConstants.INVALID_COLOR);
            }
        }

        return valid;
    }

    @Override
    public void resetInvalidProperties() {
        jmsConnectorPanel.resetInvalidProperties();
        templateTextArea.setBackground(null);
    }

    // @formatter:off
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        templateLabel = new javax.swing.JLabel();
        templateTextArea = new com.mirth.connect.client.ui.components.MirthSyntaxTextArea();
        jmsConnectorPanel = new com.mirth.connect.connectors.jms.JmsConnectorPanel();

        setBackground(new java.awt.Color(255, 255, 255));

        templateLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        templateLabel.setText("Template:");

        templateTextArea.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        templateTextArea.setToolTipText("The JMS message content.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jmsConnectorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(templateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(templateTextArea, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(50, 50, 50))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jmsConnectorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(templateLabel)
                        .addContainerGap())
                    .addComponent(templateTextArea, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mirth.connect.connectors.jms.JmsConnectorPanel jmsConnectorPanel;
    private javax.swing.JLabel templateLabel;
    private com.mirth.connect.client.ui.components.MirthSyntaxTextArea templateTextArea;
    // End of variables declaration//GEN-END:variables
    // @formatter:on
}
